using System;
using System.Threading.Tasks;
using UnityEngine;

namespace Voodoo.Meta.RVModules
{
	public class ChestVisual : MonoBehaviour
	{
		public Animator animator;

		public float timeBetweenLoop = 2f;

		private static readonly int reset = Animator.StringToHash("Show");

		private static readonly int open = Animator.StringToHash("Open");

		private static readonly int giggle = Animator.StringToHash("Giggle");

		private bool isAlive;

		public void SetUp(float delay)
		{
			animator.ResetTrigger(giggle);
			animator.ResetTrigger(open);
			animator.SetTrigger(reset);
			IdleLoop(delay);
		}

		private async void IdleLoop(float delay)
		{
			isAlive = true;
			await Task.Delay(TimeSpan.FromSeconds(delay));
			while (isAlive)
			{
				await Task.Delay(TimeSpan.FromSeconds(timeBetweenLoop));
				if (!(animator == null))
				{
					animator.SetTrigger(giggle);
					continue;
				}
				break;
			}
		}

		public void Open()
		{
			isAlive = false;
			animator.SetTrigger(open);
		}

		public void Clean()
		{
			isAlive = false;
		}
	}
}
